package com.sino.dataworks.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sino.dataworks.dto.InstanceEntityDto;
import com.sino.dataworks.util.DataworksUtil;
import com.sino.dataworks.util.JobVarUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class DataworksCheckColumnLog implements Job {


    @Override
    public void execute(JobExecutionContext jobExecutionContext)  {
        List<InstanceEntityDto> list=new ArrayList<>();
        Map<String,String> param=new HashMap<>();
        param.put("searchText","_integration");
        param.put("pageNum","1");
        param.put("pageSize","100");
        DataworksUtil.getNodeList(param);
        DataworksUtil.getDatabase("100",1);
        Integer totalNum= JobVarUtil.nodeListTotalCount;
        log.info("总条数:"+totalNum);
        int totalPages=(totalNum+100-1)/100;
        for (int i=1;i<=totalPages;i++){
            param.put("pageNum",String.valueOf(i));
            List<InstanceEntityDto> dtos=DataworksUtil.getNodeList(param);
            if(dtos!=null&&dtos.size()>0){
                list.addAll(dtos);
            }
        }
        List<Map<String, JSONObject>> list1=new ArrayList<>();
        for (InstanceEntityDto dto:list){
            Integer nodeId=dto.getNodeId();
            String nodeName=dto.getNodeName();
            String owerName=dto.getOwnerName();
            String cycTypeName=dto.getCycTypeName();
            String prgTypeName=  dto.getPrgTypeName();
            Map<String,JSONObject> map=new HashMap<>();
            if(nodeId!=null&&prgTypeName.equals("离线同步")&&nodeName.startsWith("ods_")){
                String data= DataworksUtil.getNodeCode(String.valueOf(nodeId));
                JSONArray jsonObject=JSONObject.parseObject(data).getJSONArray("steps");
                int readerCount=0;
                int writerCount=0;
                for (int i=0;i<jsonObject.size();i++){
                    JSONObject jsonObject1=jsonObject.getJSONObject(i);
                    JSONObject js=jsonObject1.getJSONObject("parameter");
                    String name=jsonObject1.getString("name");
                    if(name.equals("Reader")){
                        map.put("reader",jsonObject1);
                    }
                    if(name.equals("Writer")){
                        map.put("writer",jsonObject1);
                    }
                }
                JSONObject jsonO=new JSONObject();
                jsonO.put("fileId",dto.getFileId());
                jsonO.put("nodeId",nodeId);
                jsonO.put("nodeName",nodeName);
                map.put("fileId",jsonO);
                map.put("data", JSON.parseObject(data));
            }
            if(map.size()>0){
                list1.add(map);
            }
        }
        DataworksUtil.checkColumn(list1);
    }
}
